package com.example.user_manage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis数据库相关配置
 */
@Configuration
public class RedisConfig {

    /**
     * redis模版配置
     * @param factory redis工厂对象
     * @return redis模版对象
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        //工厂模式，创建模版
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        //指定redis的key序列化方式用字符串进行
        template.setKeySerializer(RedisSerializer.string());
        //指定redis的value序列化方式用字符串进行
        template.setValueSerializer(RedisSerializer.string());

        //指定哈希表键的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        //指定哈希表值的序列化方式
        template.setHashValueSerializer(RedisSerializer.string());
        return template;
    }
}
